<?php

define("product_paging", 3);
define("news_paging", 2);

add_filter('woocommerce_currencies', 'add_vnd');

function add_vnd($currencies){
    $currencies['vnd'] = __('Đồng', 'woocommerce');
    return $currencies;
}

add_filter('woocommerce_currency_symbol', 'add_vnd_symbol', 10, 2);

function add_vnd_symbol($currency_symbol, $currency){
    switch($currency){
        case "vnd": $currency_symbol = ''; break;
    }

    return $currency_symbol;
}

/**
Function lay ra menu
 */

if ( ! function_exists( 'get_excerpt' ) ) :
    function get_excerpt($input, $length = 200)
    {
        $input = substr(trim($input), 0, $length);
        $space_position = strrpos($input, ' ');
        echo substr($input, 0, $space_position);
    }
endif;
/**
Function lay ra menu
 */
if ( ! function_exists( 'get_menu' ) ) :

    function get_menu() {?>
    <!-- Begin #shop-menu -->
    <ul id="shop-menu" class="clearfix">
        <?php
        // $cat = get_categories("type=product_cat");
        // $cat = get_terms("product_cat");
        // var_dump($cat);

        //$args = array( 'taxonomy' => 'product_cat' );
        $terms = get_terms('product_cat', 'orderby=id&hide_empty=0&parent=0');

        $count = count($terms);
        if ( $count > 0 ){
            foreach ( $terms as $term ) {?>
                <?php //var_dump($term) ?>
                <?php echo "<li><a href='". get_term_link($term)."'><span class='item-name'>".$term->name."</span><span class='item-counter'>". get_count_of_catagory($term) ."</span></a>"; ?>

                <?php //Lay het children cua cai catalogue do ?>
                <?php $agrs = array(
                    "orderby" => "id",
                    "hide_empty" => "0",
                    "child_of" => $term->term_id,
                );?>
                <?php $term_children = get_terms('product_cat', $agrs);?>

                <?php if($term_children != null){ ?>
                    <ul class="children">
                        <?php foreach ($term_children as $term_child) {?>
                        <li><a href="<?php echo get_term_link($term_child); ?>"><span class="item-name">
                                    <?php echo $term_child->name; ?>    
                                </span><span class="item-counter"><?php echo $term_child->count; ?></span></a></li>
                        <?php } ?>
                    </ul>
                <?php } ?>

                <?php echo "</li>"; ?>
                <?php }
        }

        wp_reset_query();
        ?>
    </ul>
    <?php }
endif;

/**
Function lay ra so luong Product cua 1 category cha
 */

if( ! function_exists("get_count_of_catagory")){
    function get_count_of_catagory( $cat ){
        $agrs = array(
            "orderby" => "id",
            "hidden_empty" => "0",
            "child_of" => $cat->term_id
        );

        $term_children = get_terms("product_cat", $agrs);
        //var_dump($term_children);
        if(sizeof($term_children)>0){
            $sum = 0;
            foreach ($term_children as $term_child) {
                $sum += $term_child->count;
            }
        }

        // trường hợp riêng cho những catelogue không có con
        // trả ra tất cả sản phẩm đếm dc CHỈ trong catalogue cha
        else{
            return $cat->count;
        }

        return $sum;
    }
}

/**
Paging
 */
if ( ! function_exists("get_paging") ){
    function get_paging(){?>
    <?php global $tax; ?>
    <?php global $page; ?>
    <!--Begin .pagination .pagination-top-->
    <ul class="pagination">
        <!-- nếu có phân trang -> có sản phẩm -->
        <?php if( $tax->max_num_pages >1 ) {?>
        <?php if($page != 1){ ?>
            <li><a href="<?php echo current_url(); ?>?page=1">Đầu</a></li>
            <?php } ?>
        <?php for ($i=1; $i <= $tax->max_num_pages ; $i++) { ?>
            <li <?php if($i==$page) echo "class='active'" ?>><a href="<?php echo current_url(); ?>?page=<?php echo $i; ?>"><?php echo $i; ?></a></li>
            <?php } ?>
        <?php if($page != $tax->max_num_pages) {?>
            <li><a href="<?php echo current_url(); ?>?page=<?php echo $tax->max_num_pages; ?>">Cuối</a></li>
            <?php } ?>
        <?php } ?>
    </ul>
    <!--End .pagination .pagination-top-->
    <?php }
}


/**
Function lay ra 3 tin tuc moi nhat o trang chu
 */
if( !function_exists("get_homepage_news") ){
    function get_homepage_news(){?>

    <!-- lay ra 3 san pham moi nhat  -->
    <?php
        $posts = get_posts("category_name=news&post_type=post&showposts=3&sortby=id");
        ?>
    <?php //var_dump($posts); ?>
    <?php foreach ($posts as $post) {?>
        <!-- lay hinh dai dien cua bai post -->
        <a href="<?php echo $post->guid; ?>"><?php echo get_the_post_thumbnail($post->ID, $size = 'full', $attr = '');?></a>
        <?php } ?>
    <?php }
}

function get_size( $post_id, $before = "", $after = ", "){
    
    // kiểm tra có cái pa_size này tồn tại ko đã
    $check = get_terms("pa_size");
    
    // nếu kiểm tra có thì mới print ra, tránh trường in ra 1 nùi lỗi
    if( ! $check->errors ){
        $terms = wp_get_post_terms($post_id, "pa_size");
        // echo sizeof($terms);
        $count = count($terms)-1;
        for($i = 0; $i < $count; $i++) {
            echo $before . $terms[$i]->name . $after;
        }
        echo $before . $terms[$count]->name;
    }
}

if( ! function_exists(current_url) ){
    function current_url(){
        global $wp;
        return home_url( $wp->request );
    }
}